//
//  LLGEmoticonKeyboard.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/18.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit

//重用标示
let EmoticonCollectionViewCellIdentifier = "EmoticonCollectionViewCellIdentifier"

class LLGEmoticonKeyboard: UIView {
    
    //toolBar
    private lazy var toolBar: LLGEmoticonToolBar = {
    
        let toolBar = LLGEmoticonToolBar(frame: CGRectZero)
        
        return toolBar
    }()
    
    //表情视图
    private lazy var emoticonCollectionView: UICollectionView = {
    
        //布局方式
        let flowLayout = UICollectionViewFlowLayout()
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        
        //隐藏滚动指示器
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        
        //分页
        view.pagingEnabled = true
        //取消弹簧
        view.bounces = false
        
        
        //注册cell
        view.registerClass(LLGEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: EmoticonCollectionViewCellIdentifier)
        
        view.backgroundColor = RandomColor()
        
        view.dataSource = self
        
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: --添加控件,设置约束
    private func setupUI() {
    
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        //添加控件
        addSubview(emoticonCollectionView)
        addSubview(toolBar)
        
        //设置约束
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        toolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.leading.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        
        //闭包
        toolBar.didSelectedButtonClosure = { [weak self] (type: LLGEmoticonToolBarButtonType) in
            
            switch type {
            case .Normal:
                print("默认")
            case .Emoji:
                print("emoji")
            case .LXH:
                print("浪小花")
            }
        
        }
    }
    
    // MARK: --给子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = emoticonCollectionView.size
        //水平间距
        flowLayout.minimumInteritemSpacing = 0
        //垂直间距
        flowLayout.minimumLineSpacing = 0
        //水平方向滚动
        flowLayout.scrollDirection = .Horizontal
    }
}

//数据源方法
extension LLGEmoticonKeyboard: UICollectionViewDataSource {

    // MARK: --组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 3
    }
    
    // MARK: --行
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    
    // MARK: --cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EmoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! LLGEmoticonCollectionViewCell
        
        cell.indexPath = indexPath
        
        cell.backgroundColor = RandomColor()
        
        return cell
    }
    
}
